package com.teach.servlet.admin;


import com.teach.dao.SportsmanInfoDAO;
import com.teach.dao.impl.SportsmanInfoDAOImpl;
import com.teach.entity.SportsmanInfo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/admin/sportsman-add.let")
public class SportsmanAddServlet extends HttpServlet {
    private SportsmanInfoDAO dao = new SportsmanInfoDAOImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.reset();
        response.setContentType("application/json;charset=UTF-8");
        try {
            SportsmanInfo e = new SportsmanInfo();


            e.setSportsmanId(null);
            e.setCountryCode(request.getParameter("countryCode"));
            e.setSportsmanName(request.getParameter("sportsmanName"));
            SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
            Date d = sd.parse(request.getParameter("sportsmanBrithdate"));
            e.setSportsmanBrithdate(d);
            e.setSportsmanHeight(Integer.valueOf(request.getParameter("sportsmanHeight")));
            e.setSportsmanWeigth(new BigDecimal(request.getParameter("sportsmanWeigth")));
            e.setSportsmanUri(request.getParameter("sportsmanUri"));
            e.setSportsmanDesc(request.getParameter("sportsmanDesc"));

            try {
                dao.insert(e);
                response.getWriter().print("{\"ok\":true}");
            } catch (Exception ex) {

            }

        }catch (Exception ex){
            response.getWriter().print("{\"ok\":false,\"msg\":\""+ex.getMessage()+"\"}");
        }

        response.flushBuffer();
    }
}
